using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace LDNS.SDK.Commands.Models
{
    /// <summary>
    /// 获取缓存统计信息命令类
    /// 作者: huobing
    /// </summary>
    public sealed class GetCacheStatsCommand : BaseCommand<GetCacheStatsResult>
    {
        /// <summary>
        /// 命令名称
        /// </summary>
        public const string NAME = "GET_CACHE_STATS";
        
        /// <summary>
        /// 获取命令名称
        /// </summary>
        public override string Name => NAME;
        
        /// <summary>
        /// 获取命令格式
        /// </summary>
        public override string Format => "GET_CACHE_STATS";
        
        /// <summary>
        /// 获取命令描述
        /// </summary>
        public override string Description => "获取缓存统计信息";
        
        /// <summary>
        /// 获取命令权限级别
        /// </summary>
        public override CommandPermission Permission => CommandPermission.User;
        
        /// <summary>
        /// 验证命令参数
        /// </summary>
        /// <param name="args">命令参数数组</param>
        /// <param name="error">错误信息输出</param>
        /// <returns>验证是否通过</returns>
        public override bool Validate(string[] args, out string error) 
        {
            error = string.Empty;
            return args.Length == 0 || (error = "此命令不接受参数") == string.Empty;
        }
    }
    
    /// <summary>
    /// 缓存统计信息命令的返回实体类
    /// 作者: huobing
    /// </summary>
    public class GetCacheStatsResult : BaseCommandResult
    {
        /// <summary>
        /// 缓存项总数
        /// </summary>
        public int TotalItems { get; set; }
        
        /// <summary>
        /// 缓存命中率
        /// </summary>
        public double HitRate { get; set; }
        
        /// <summary>
        /// 缓存大小（字节）
        /// </summary>
        public long CacheSize { get; set; }
        
        /// <summary>
        /// 缓存项类型分布
        /// </summary>
        public Dictionary<string, int> RecordTypeDistribution { get; set; } = new Dictionary<string, int>();
        
        /// <summary>
        /// 最近访问的缓存项数量
        /// </summary>
        public int RecentlyAccessedCount { get; set; }
        
        /// <summary>
        /// 控制台输出格式化
        /// </summary>
        /// <returns>格式化后的缓存统计信息</returns>
        public override string ToConsoleString()
        {  
            var sb = new System.Text.StringBuilder();
            
            if (!string.IsNullOrEmpty(Message))
            {
                sb.AppendLine(Message);
            }
            else
            {
                sb.AppendLine("缓存统计信息：");
            }
            
            sb.AppendLine($"总缓存项数: {TotalItems}");
            sb.AppendLine($"缓存命中率: {HitRate:P2}");
            sb.AppendLine($"缓存大小: {FormatSize(CacheSize)}");
            sb.AppendLine($"最近访问的缓存项数量: {RecentlyAccessedCount}");
            
            if (RecordTypeDistribution != null && RecordTypeDistribution.Count > 0)
            {
                sb.AppendLine("\n记录类型分布：");
                
                // 准备表格数据
                var tableData = new List<string[]>();
                tableData.Add(new[] { "记录类型", "数量" }); // 表头
                
                foreach (var kvp in RecordTypeDistribution)
                {
                    tableData.Add(new[]
                    {
                        kvp.Key,
                        kvp.Value.ToString()
                    });
                }
                
                // 使用TextTableFormatter格式化表格
                string table = LDNS.SDK.Formatting.TextTableFormatter.FormatAlignedTable(tableData, ' ', 80);
                sb.AppendLine(table);
            }
            
            return sb.ToString().TrimEnd();
        }
        
        /// <summary>
        /// 格式化文件大小
        /// </summary>
        /// <param name="bytes">字节数</param>
        /// <returns>格式化后的大小字符串</returns>
        private string FormatSize(long bytes)
        {
            if (bytes < 1024)
                return $"{bytes} B";
            else if (bytes < 1048576)
                return $"{bytes / 1024.0:F2} KB";
            else if (bytes < 1073741824)
                return $"{bytes / 1048576.0:F2} MB";
            else
                return $"{bytes / 1073741824.0:F2} GB";
        }
    }
}